<template>
<div>
  <game-type-list @onSelected="getSelectList" :opens="handleSelect"></game-type-list>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属用户" prop="userId">
        <el-input
            v-model="queryParams.userId"
            placeholder="请输入所属用户"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类ID" prop="categoryId">
        <el-select v-model="queryParams.categoryId" placeholder="请选择分类ID" clearable>
          <el-option
              v-for="dict in game_platform"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="游戏账号名称" prop="accountName">
        <el-input
            v-model="queryParams.accountName"
            placeholder="请输入游戏账号名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['gameList:gametype:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['gameList:gametype:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['gameList:gametype:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['gameList:gametype:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="gametypeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="账号ID" align="center" prop="accountId" />-->
      <el-table-column label="所属用户" align="center" prop="userName"/>
      <el-table-column label="游戏分类" align="center" prop="categoryName"/>
      <el-table-column label="游戏平台" align="center" prop="platform">
        <template #default="scope">
          <dict-tag :options="game_platform" :value="scope.row.platform"/>
        </template>
      </el-table-column>
      <el-table-column label="游戏账号名称" align="center" prop="accountName"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['gameList:gametype:edit']">修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['gameList:gametype:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改游戏账号，存储用户的游戏账号信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="gametypeRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属用户" prop="userId">
          <el-input v-model="form.userId" placeholder="请选择授权用户"/>
        </el-form-item>


        <!--        <el-form-item label="游戏分类" prop="categoryName">-->
        <!--          <el-input v-model="form.categoryName" placeholder="请选择游戏分类" />-->
        <!--        </el-form-item>-->

<!--                <el-form-item label="游戏平台" prop="platform" >-->
<!--                  <el-select v-model="form.platform" placeholder="请选择游戏平台" disabled>-->
<!--                    <el-option-->
<!--                        v-for="dict in game_platform"-->
<!--                        :key="dict.value"-->
<!--                        :label="dict.label"-->
<!--                        :value="parseInt(dict.value)"-->
<!--                    ></el-option>-->
<!--                  </el-select>-->
<!--                </el-form-item>-->


        <el-form-item label="游戏类型" prop="categoryName">
          <el-input v-model="form.categoryName" placeholder="请选择游戏分类">
            <template #append>
              <el-button @click="openSelect">
                选择
              </el-button>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label="游戏账号" prop="accountName">
          <el-input v-model="form.accountName" placeholder="请输入游戏账号名称"/>
        </el-form-item>
        <el-form-item label="游戏密码" prop="accountPassword">
          <el-input v-model="form.accountPassword" placeholder="请输入游戏密码"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</div>
</template>

<script setup name="Gametype">
import {listGametype, getGametype, delGametype, addGametype, updateGametype} from "@/api/game/gametype";
import {listGames} from "@/api/game/games.js";
import gameTypeList from "@/components/gameList/gameListType.vue";

const {proxy} = getCurrentInstance();
const {game_platform} = proxy.useDict('game_platform');


const handleSelect = ref(false)
// const cancelSelect = function () {
//   handleSelect.value = false
// }
const openSelect = function () {
  handleSelect.value = true
}
const getSelectList = function (e) {
  let a = toRaw(e);
  console.log(a,"data")
  form.value.categoryId = a.categoryId
  form.value.platform = a.platform
  form.value.categoryName = a.categoryName
  handleSelect.value = false
}


const gametypeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const ca = ref();
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    categoryId: null,
    accountName: null,
  },
  rules: {
    userId: [
      {required: true, message: "所属用户不能为空", trigger: "blur"}
    ],
    categoryId: [
      {required: true, message: "分类ID不能为空", trigger: "change"}
    ],
    accountName: [
      {required: true, message: "游戏账号名称不能为空", trigger: "blur"}
    ],
    accountPassword: [
      {required: true, message: "游戏密码不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询游戏账号，存储用户的游戏账号信息列表 */
function getList() {
  loading.value = true;
  listGametype(queryParams.value).then(response => {
    gametypeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    accountId: null,
    userId: null,
    categoryId: null,
    accountName: null,
    accountPassword: null,
    createTime: null,
    platform: null,
    categoryName: null
  };
  proxy.resetForm("gametypeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.accountId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  listGames().then(response => {
    let data = response.rows;
    let categoryNameList = data.map(item => {
      return item["categoryName"]
    });
    ca.value = categoryNameList;
  })
  open.value = true;
  title.value = "添加游戏账号";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  console.log(game_platform)
  const _accountId = row.accountId || ids.value
  getGametype(_accountId).then(response => {
    console.log(response)
    form.value = response.data;
    open.value = true;
    title.value = "修改游戏账号，存储用户的游戏账号信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["gametypeRef"].validate(valid => {
    if (valid) {
      if (form.value.accountId != null) {
        updateGametype(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addGametype(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _accountIds = row.accountId || ids.value;
  proxy.$modal.confirm('是否确认删除游戏账号，存储用户的游戏账号信息编号为"' + _accountIds + '"的数据项？').then(function () {
    return delGametype(_accountIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('game/gametype/export', {
    ...queryParams.value
  }, `gametype_${new Date().getTime()}.xlsx`)
}

getList();
</script>
